var manager = new vo.newObj({
	parent:'#manager',
	el:'#manager',
	datas:{num:1,
		size:9,
		total:0,
		page:'<a>1</a>',
		pages:0,
		content:"<span class='loading' >loading...</span>",
		classify:"<span class='loading'>loading...</span>",
		clyDiv:getElementsByAttr_r(document, "class", "div-cly")[0],
		selectId:'',
		articles:{}
	},
	methods:{
		load:function(datas){
			var send = {};
			send['num'] = datas['num'];
			send['size'] = datas['size'];
			send['total'] = datas['total'];
			manager.modify("content","<span class='loading' >loading...</span>");
			ajax({
				method:'GET',
				url:'admin/article/pager',
				datas:send,
				success:function(resp){
					if(resp.status == 200){
						var datas = resp.datas;
						var articles = datas.datas;
						var articleStore = manager.get("articles");
						var content = "";
						for(var i = 0;i<articles.length;i++){
							articleStore[articles[i].id] = articles[i];
							content+=article(articles[i]);
						}
						manager.modify("content",content);
						for(var name in send){
							manager.modify(name,datas[name]);
						}
						var page = "";
						for(var i = datas.begin;i<=datas.end;i++){
							if(i == datas.num){
								page+=("<a vo-m='click:locate' class='pager-select num' idx='"+i+"'>"+i+"</a>");
							}else{
								page +=("<a vo-m='click:locate' idx='"+i+"'>"+i+"</a>");
							}
						}
						var selectId = manager.get("selectId");
						if(selectId !=""){
							getElementsByAttr_r(manager.el, "nid", selectId)[0].setAttribute("class","article on");
							mgrStatus.methods.flush(articleStore[selectId]);
						}else{
							mgrStatus.methods.init();
						}
						manager.modify("page",page);
						manager.modify("pages",datas['pages']);
					}
				},
				error:function(){
					throw new Error("加载文章失败");
				}
			});
		},
		back:function(datas){
			if(datas.num>1){
				this.modify("selectId","");
				this.modify("num",datas.num-1);
				this.methods.load();
			}
		},
		next:function(datas){
			if(datas.num != datas.pages){
				this.modify("selectId","");
				this.modify("num",datas.num+1);
				this.methods.load();
			}
		},
		locate:function(datas){
			this.modify("selectId","");
			this.modify("num",target().getAttribute("idx"));
			this.methods.load();
		},
		end:function(datas){
			this.modify("selectId","");
			this.modify("num",datas.pages);
			this.methods.load();
		},
		check:function(datas){
			var t = target();
			if(t.getAttribute("class") == "article"){
				t.setAttribute("class","article on");
				var idx = t.getAttribute("nid");
				var els = getElementsByAttr_r(this.el, "nid", "\\w+");
				for(var i = 0;i<els.length;i++){
					var el = els[i];
					if(el.getAttribute("nid") != idx){
						el.setAttribute("class","article");
					}
				}
				var article = datas.articles[idx];
				this.modify("selectId",idx);
				mgrStatus.methods.flush(article);
			}
		},
		status:function(datas,t,status){//更新文章的状态
			if(typeof(t) == 'undefined'){
				var t = target();
			}
			if(typeof(status) == 'undefined'){
				var status = t.getAttribute("status");
			}
			if("true" == t.getAttribute("pending")){
				return;
			}
			t.setAttribute("pending","true");
			var content = t.innerHTML;
			var style = t.getAttribute("style");
			t.innerHTML = "正在处理....";
			t.setAttribute("style",style+";background:#999999");
			var id = t.parentNode.getAttribute("nid");
			var chgStatus = t.getAttribute(status);
			var article = datas.articles[id];
			article[status] = parseInt(chgStatus);
			var send = {};
			send['irelease'] = article['irelease'];
			send['visiable'] = article['visiable'];
			send['icommand'] = article['icommand'];
			ajax({
				method:'PUT',
				url:'admin/article/status/'+id,
				datas:send,
				success:function(resp){
					if(resp.status == 200){
						manager.methods.load();
					}
					t.setAttribute("style",style);
					t.innerHTML = content;
					t.setAttribute("pending","false");
				},
				error:function(mesg){
					alert("发生不可预料错误");
					t.setAttribute("style",style);
					t.innerHTML = content;
					t.setAttribute("pending","false");
				}
			});
		},
		edit:function(datas){
			var t = target();
			var id = t.parentNode.getAttribute("nid");
			if(id!=null){
				console.log(id);
				if(t.getAttribute("class").indexOf("disabled")<0||datas.articles[id].irelease == "0"){
					window.location = basepath+"admin/article/editor/"+id;
				}
			}
		},
		cly:function(datas){
			if(datas.clyDiv.style.display == "none"){
				datas.clyDiv.style.display = "block";
				ajax({
					method:'GET',
					url:'admin/article/classify',
					success:function(resp){
						if(resp.status == 200){
							var content = "";
							var datas = resp.datas;
							for(var i = 0;i<datas.length;i++){
								content+="<div class='cly' cly='"+datas[i].id+"' vo-m='click:newOne'>["+datas[i].name+"]</div>";
							}
							manager.modify("classify",content);
						}
					},
					error:function(){
						throw new Error("加载类别失败");
					}
				});
			}else if(datas.clyDiv.style.display == "block"){
				datas.clyDiv.style.display = "none";
				this.modify("classify","<span class='loading'>loading...</span>");
			}
		},
		newOne:function(datas){
			var t = target();
			if(t.getAttribute("class") == "cly"){
				var map = {};
				map['classifyId'] = t.getAttribute("cly");
				var json = JSON.stringify(map);
				window.location = basepath+"admin/article/editor?"+json;
			}
		}
	}
});
var mgrStatus = new vo.newObj({
	el:'#status',
	datas:{
		type:'',
		classify:'',
		comments:'',
		readers:'',
		visiable:'',
		icommand:'',
		releaseTime:'',
		createTime:'',
		id:'',
		setCommand:'设为推荐'
	},
	methods:{
		command:function(){
			if(target().getAttribute("icommand")!=null){
				manager.methods.status(target());
			}
		},
		init:function(datas){
			for(var name in datas){
				if(name == "setCommand"){
					this.modify("setCommand","设为推荐");
					var command = getElementsByAttr_r(this.el, "name", "setCommand")[0];
					command.setAttribute("class","button disabled");
				}else{
					this.modify(name,"");
				}
			}
		},
		flush:function(datas,outer){
			console.log(outer);
			for(var name in outer){
				var value = "";
				if(name == "type"){
					if(outer[name] == "0"){
						value = "原文";
					}else if(outer[name] == "1"){
						value = "转载";
					}else{
						value = "收藏";
					}
				}else if(name == "classify"){
					value = outer[name].name;
				}else if(name == "visiable" || name == "icommand"){
					if(outer[name] == "0"){
						value = "否";
					}else{
						value = "是";
					}
				}else if(name == "releaseTime" || name == "createTime"){
					value = new Date(parseInt(outer[name])).toLocaleString().replace(/:\d{1,2}$/,'');
				}else{
					value = outer[name];
				}
				this.modify(name,value);
			}
			var command = getElementsByAttr_r(this.el, "name", "setCommand")[0];
			command.setAttribute("class","button active");
			this.el.setAttribute("nid",outer['id']);
			if(datas['icommand'] == "是"){
				this.modify('setCommand',"取消推荐");
				command.setAttribute("icommand",0);
			}else{
				this.modify('setCommand',"设为推荐");
				command.setAttribute("icommand",1);
			}
		}
	}
});
manager.methods.load();
var article = function(){
	var model = "<div class='article' nid='${id}' vo-m='click:check'><span class='icon opacity btn-border'>${type}</span>" +
		"<span class='title'>${title}</span>"+
		"<span class='editInfo'>最后编辑于:${updateTime}</span>"+
		"<div class='button send ${sendStatus}' vo-m='click:status' status='irelease' irelease='${chgStatus}'>${sendStr}</div>"+
		"<div class='button edit ${editStatus}' vo-m='click:edit'>编辑</div>"+
		"</div>";
	function create(data){
		var copy = model;
		if(data.irelease == 1){
			data['editStatus'] = "disabled";
			data['sendStr'] = "取消发布";
			data['chgStatus'] = 0;
		}else{
			data['editStatus'] = "active";
			data['sendStr'] = "发布";
			data['chgStatus'] = 1;
		}
		data['sendStatus'] = 'active';
		var reg = new RegExp("\\${(\\w+)}?","g");
		var result = copy.replace(reg,function($0,$1){
			if($1 == "type"){
				if(data[$1] == "0"){
					return "原";
				}else if(data[$1] == "1"){
					return "转";
				}else if(data[$1] == "2"){
					return "藏";
				}
			}else if($1 == "updateTime"){
				return new Date(parseInt(data[$1])).toLocaleString().replace(/:\d{1,2}$/,'');
			}else{
				return data[$1];
			}
		});
		return result;
	}
	return create;
}();